package server.data;

import java.io.Serializable;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Danon
 */
public class Customer implements Serializable {
    public final long serialVersionID = 1L;
    
    private int id;
    private String name;

    public Customer(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Customer{name:" + name + "; id:"+id+"}";
    }
    
    public static Customer createFromDOM(Node node) {
        NamedNodeMap m = node.getAttributes();
        Node idNode = m==null ? null : m.getNamedItem("id");
        int id = idNode==null ? -1 : Integer.parseInt(idNode.getTextContent());
        NodeList childNodes = node.getChildNodes();
        Node nameNode = null;
        if(childNodes!=null)
            for(int i = 0; i<childNodes.getLength(); i++)
                if(childNodes.item(i).getNodeName().equals("name"))
                    nameNode = childNodes.item(i);
        String name = null;
        if(nameNode.hasChildNodes())
            name = nameNode.getChildNodes().item(0).getTextContent();
        if(name==null)
            throw new IllegalArgumentException();
        return new Customer(id, name);
    }
    
}
